<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>Kura's Hardware Shop</title>

        <!-- Bootstrap Core CSS -->
        <link href="css/bootstrap.min.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="css/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="css/sb-admin-2.css" rel="stylesheet">

        <!-- Custom Fonts -->
        <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->


        <!-- jQuery Version 1.11.0 -->
        <script src="js/jquery-1.11.0.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="js/bootstrap.min.js"></script>

        <!-- Metis Menu Plugin JavaScript -->
        <script src="js/plugins/metisMenu/metisMenu.min.js"></script>

        <!-- Custom Theme JavaScript -->
        <script src="js/sb-admin-2.js"></script>
    </head>

    <body><script>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(1);
require("./script/dbconnect.php");
session_start();
$info = false;
$passchange = false;
if (!isset($_SESSION['id'])) {
    header("Location: login.php");
} else {
    echo "$(document).ready(function(){";

    if (isset($_POST['save'])) {
        $time = DateTime::createFromFormat('d/m/Y', $_POST['user_birthday']);
		if ($time !== false) {
			$_POST['user_birthday'] = $time->format('Y-m-d');

			$update_sql = "UPDATE `users` SET `email`='{$_POST['user_email']}',`name`='{$_POST['user_name']}',`address`='{$_POST['user_address']}',`phone`='{$_POST['user_phone']}',`gender`={$_POST['user_gender']},`birthday`='{$_POST['user_birthday']}',`id_number`='{$_POST['user_id']}',`role`={$_POST['user_role']} WHERE `id` = {$_SESSION['id']};";

			$oldpass = $_POST['user_old_pass'];
			// start to handle password change
			if (strlen(trim($oldpass)) > 0) {
				$oldpass = md5($_POST['user_old_pass']);
				
				$check_sql = "SELECT * FROM `users` WHERE `id` = {$_SESSION['id']} AND `password` = '$oldpass';";
				if ($result = mysqli_query($db, $check_sql)) {
					if (mysqli_num_rows($result) > 0) {
						$newpass = $_POST['user_new_pass'];
						if (strlen(trim($newpass)) >= 6) {
							$newpass = $_POST['user_new_pass'];
							$newpass_retype = $_POST['user_new_pass_retype'];
							if ($newpass === $newpass_retype) {
								$newpass = md5($_POST['user_new_pass']);
								$update_sql .= " UPDATE `users` SET `password` = '$newpass' WHERE `id` = {$_SESSION['id']};";

								$passchange = true;
							} else {
								echo "$('#alert-message').append(\"<div class='alert alert-danger' role='alert'>Thay đổi mật khẩu: Nhập lại mật khẩu mới không khớp</div>\");";
							}
						} else {
							echo "$('#alert-message').append(\"<div class='alert alert-danger' role='alert'>Thay đổi mật khẩu: Độ dài mật khẩu mới phải ít nhất 6 kí tự</div>\");";
						}
					} else {
						echo "$('#alert-message').append(\"<div class='alert alert-danger' role='alert'>Thay đổi mật khẩu: Mật khẩu hiện tại không đúng</div>\");";
					}
					mysqli_free_result($result);
				}
			}

			if (mysqli_multi_query($db, $update_sql)) {
				$_SESSION['name']= $_POST['user_name'];
				echo "$('#alert-message').append(\"<div class='alert alert-success' role='alert'>Thông tin cá nhân: Cập nhật thành công</div>\");";
				if ($passchange) {
					echo "$('#alert-message').append(\"<div class='alert alert-success' role='alert'>Thay đổi mật khẩu: Mật khẩu mới được cập nhật thành công</div>\");";
				}
				do {
					if ($result = mysqli_store_result($db)) {
						mysqli_free_result($result);
					}
				} while (mysqli_next_result($db));
			} else {
				echo "$('#alert-message').append(\"<div class='alert alert-danger' role='alert'>Cập nhật thông tin xảy ra lỗi: " . mysqli_error($db) . "</div>\");";
			}
		} else {
			echo "$('#alert-message').append(\"<div class='alert alert-danger' role='alert'>Cập nhật thông tin xảy ra lỗi: Định dạng thời gian sai, định dạng đúng: dd/mm/yyyy</div>\");";
		}
    }

    $sql = "SELECT * FROM `users` WHERE `id` = {$_SESSION['id']};";
    if ($result = mysqli_query($db, $sql)) {
        $row = mysqli_fetch_assoc($result);
        $info['email'] = $row['email'];
        $info['name'] = $row['name'];
        $info['gender'] = $row['gender'];
        $info['address'] = $row['address'];
        $info['phone'] = $row['phone'];
        $info['id_number'] = $row['id_number'];
        $info['role'] = $row['role'];

        $info['birthday'] = $row['birthday'];
        $time = DateTime::createFromFormat('Y-m-d', $row['birthday']);
        $info['birthday'] = $time->format('d/m/Y');
    }
    echo "});";
}
?>
        </script>
        <div id="wrapper">

            <!-- Navigation -->
            <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="index.html">Xin chào <?php echo $_SESSION['name'] ?></a> <!-- get User late -->
                </div>
                <!-- /.navbar-header -->

                <ul class="nav navbar-top-links navbar-right">

                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-user fa-fw"></i>  <i class="fa fa-caret-down"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="info.php"><i class="fa fa-user fa-fw"></i> Thông tin cá nhân</a>
                            </li>

                            <li class="divider"></li>
                            <li><a href="script/logoutScript.php"><i class="fa fa-sign-out fa-fw"></i> Đăng xuất</a>
                            </li>
                        </ul>
                        <!-- /.dropdown-user -->
                    </li>
                    <!-- /.dropdown -->
                </ul>
                <!-- /.navbar-top-links -->

                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <ul class="nav" id="side-menu">
                            <?php
                            if ($_SESSION['role'] == 1) {
                                ?>
                                <li>
                                    <a class="active" href="statistic.php"><i class="fa fa-bar-chart-o fa"></i> &nbspThống kê<span class="fa arrow"></span></a>
                                    <ul class="nav nav-second-level">
                                        <li>
                                            <a href="statistic.php">Doanh thu</a>
                                        </li>
                                    </ul>
                                </li>

                                <li>
                                    <a href="users.php"><i class="fa fa-user fa-fw"></i> Quản lí nhân viên</a>

                                    <!-- /.nav-second-level -->
                                </li>
                                <?php
                            }
                            ?>
                            <li>
                                <a href="customers.php"><i class="fa fa-wrench fa-fw"></i>&nbsp;Quản lí khách hàng</a>

                                <!-- /.nav-second-level -->
                            </li>
                            <li>
                                <a href="#"><i class="fa fa-shopping-cart fa-fw"></i> Quản lí sản phẩm<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="categories.php">Danh mục</a>
                                    </li>
                                    <li>
                                        <a href="manufacturers.php">Nhà sản xuất</a>
                                    </li>                                    
                                    <li>
                                        <a href="products.php">Sản phẩm</a>
                                    </li>
                                </ul>
                            </li>
                            <li>

                                <a href="#"><i class="fa fa-edit fa-fw"></i> Quản lí đơn hàng<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="orders_in.php">Hóa đơn nhập</a>
                                    </li>
                                    <li>
                                        <a href="orders_out.php">Hóa đơn xuất</a>
                                    </li>

                                </ul>
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </div>
                    <!-- /.sidebar-collapse -->
                </div>
                <!-- /.navbar-static-side -->
            </nav>

            <!-- Page Content -->
            <div id="page-wrapper">
                <form role="form" method=POST>
                    <div class="row">
                        <div class="col-lg-12">
                            <h1 class="page-header">Thông tin cá nhân</h1>
                        </div>
                        <!-- /.col-lg-12 -->
                    </div>
                    <!-- /.row -->	
                    <div class='row'>
                        <div class='col-lg-12' id="alert-message">

                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-lg-4">
                            <input class="form-control" type="text" name="user_name" placeholder="Họ tên" value="<?php if ($info !== false) echo $info['name']; ?>">
                        </div>
                        <div class="col-lg-2">
                            <div class="form-group">
                                <select name="user_role" class="form-control">
                                    <option value=1 <?php if ($info !== false) if ($info['role'] == 1) echo "selected"; ?>>Admin</option>
                                    <option value=2 <?php if ($info !== false) if ($info['role'] == 2) echo "selected"; ?>>Nhân viên</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-lg-3">
                            <div class="form-group">
                                <label>Giới tính: </label>
                                <label class="radio-inline">
                                    <input type="radio" name="user_gender" value=1 <?php if ($info !== false) if ($info['gender'] == 1) echo "checked"; ?>>Nam
                                </label>
                                <label class="radio-inline">
                                    <input type="radio" name="user_gender" value=0 <?php if ($info !== false) if ($info['gender'] == 0) echo "checked"; ?>> Nữ
                                </label>
                            </div>
                        </div>
                    </div>
                    <!-- /.row -->

                    <br>
                    <div class="row">
                        <div class="col-lg-4">
                            <div class="form-group input-group">
                                <span class="input-group-addon">@</span>
                                <input class="form-control" type="email" name="user_email" placeholder="Email" value="<?php if ($info !== false) echo $info['email']; ?>">
                            </div>
                        </div>
                    </div>
                    <!-- /.row -->

                    <br>
                    <div class="row">
                        <div class="col-lg-2">
                            <div class="form-group">
                                <label>Ngày sinh: </label>
                                <input class="form-control" type="text" name="user_birthday" placeholder="dd/mm/yyyy" value="<?php if ($info !== false) echo $info['birthday']; ?>">
                            </div>
                        </div>
                        <div class="col-lg-2">
                            <div class="form-group">
                                <label>Số CMND: </label>
                                <input class="form-control" type="text" name="user_id" placeholder="Số CMND" value="<?php if ($info !== false) echo $info['id_number']; ?>">
                            </div>
                        </div>
                        <div class="col-lg-2">
                            <div class="form-group">
                                <label>Số điện thoại: </label>
                                <input class="form-control" type="text" name="user_phone" placeholder="Số điện thoại" value="<?php if ($info !== false) echo $info['phone']; ?>">
                            </div>
                        </div>
                    </div>
                    <!-- /.row -->

                    <br>
                    <div class="row">
                        <div class="col-lg-6">
                            <div class="form-group">
                                <label>Địa chỉ: </label>
                                <input class="form-control" type="text" name="user_address" placeholder="Địa chỉ" value="<?php if ($info !== false) echo $info['address']; ?>">
                            </div>
                        </div>
                    </div>
                    <!-- /.row -->

                    <br>
                    <div class="row">
                        <div class="col-lg-3">
                            <div class="form-group">
                                <label>Đổi mật khẩu: </label>
                                <input class="form-control" type="password" name="user_old_pass" placeholder="Mật khẩu hiện tại"><br>
                                <input class="form-control" type="password" name="user_new_pass" placeholder="Mật khẩu mới"><br>
                                <input class="form-control" type="password" name="user_new_pass_retype" placeholder="Nhập lại mật khẩu mới"><br>
                            </div>
                        </div>
                    </div>
                    <!-- /.row -->

                    <div class="row">
                        <div class="col-lg-8">
                            <button type=submit class="btn btn-primary" style="float:right;" name="save">Lưu</button><br>
                        </div>
                    </div>
                    <br>
                    <br>
                </form>
            </div>
            <!-- /#page-wrapper -->

        </div>
        <!-- /#wrapper -->

    </body>

</html>